// 导入静态路由
import { constantRoutes, asyncRoutes } from '@/router'

// 权限模块
const state = {
  routes: constantRoutes
}

const mutations = {
  // 设置动态路由
  setRoutes(state, newRoutes) {
    state.routes = [
      ...constantRoutes,
      ...newRoutes,
      {
        path: '*',
        redirect: './404',
        hidden: true
      }
    ]
  }
}

const actions = {
  // 前端筛选路由，data就是后端返回的菜单权限标识
  filterRoutes(context, data) {
    const routes = [] //   筛选出 动态路由中和data中能够对上的路由
    data.forEach((item) => {
      // item是每个菜单权限标识，就是name
      // const row = asyncRoutes.find(val =>val.name ===item)
      // row && routes.push(row)
      console.log(...asyncRoutes.filter((val) => val.name === item))
      routes.push(...asyncRoutes.filter((val) => val.name === item))
    })
    console.log(data)
    console.log(asyncRoutes)
    console.log(routes)
    context.commit('setRoutes', routes)
    return routes // 这里为什么还要return  state数据 是用来 显示左侧菜单用的  return  是给路由addRoutes用的
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
